package com.qf.test;

import com.qf.mapper.UserMapper;
import com.qf.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create by 周志豪 on 2022/8/23  19:40
 *
 * @authoe 周志豪
 * @veision 1.0
 * //TODO  想好了再去做
 * @since 1.0
 */
public class testMain {

    private UserMapper mapper;
    InputStream inputStream;
    SqlSessionFactory sessionFactory;
    SqlSession sqlSession;
//在测试之前运行
    @Before
    public void before () throws IOException {
        //加载配置文件
        inputStream= Resources.getResourceAsStream("mybatis.xml");
        //用配置文件创建数据库的会话工厂
        sessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        //打开一个会话
        sqlSession=sessionFactory.openSession();
        //创建接口对象
        mapper = sqlSession.getMapper(UserMapper.class);
    }
    //测试之后运行
    @After
    public void commit(){
        sqlSession.commit();//提交事务，不然增删的操作不会改变数据库里面的内容
        sqlSession.close();
    }

    @Test
    public void testFindById() throws IOException {
        User user = mapper.findById(1);
        System.out.println(user);
    }

    @Test
    public void testFindByNamePassword() throws IOException {
        User user = mapper.findByNamePwd("张三","123");
        System.out.println(user);
    }

    @Test
    public void testFindByNamePasswordMap() throws IOException {
        Map map=new HashMap();
        map.put("username","张三");
        map.put("password","123");
        User user = mapper.findByNamePwdMap(map);
        System.out.println(user);
    }

    @Test
    public void testfindByFirstName() throws IOException {
        User user = mapper.findByFirstName("王");
        System.out.println(user);
    }

    @Test
    public void testfindAll() throws IOException {
        List<User> lists = mapper.findAll();
        for (User list:lists
             ) {
            System.out.println(list);
        }
    }

}
